package ynu.edu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import ynu.edu.domain.PageDTO;
import ynu.edu.domain.R;
import ynu.edu.domain.dto.Img2ImgRequest;
import ynu.edu.domain.vo.Img2ImgResponse;
import ynu.edu.entity.Img2Img;
import ynu.edu.service.Img2ImgService;

import javax.validation.Valid;
import java.util.*;

/**
 * Img2Img控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/img2img")
@RequiredArgsConstructor
@Api(tags = "图片生成图片接口")
public class Img2ImgController {

    private final Img2ImgService img2ImgService;

    /**
     * 执行动漫风格图片生成
     */
    @PostMapping("/anime")
    @ApiOperation("执行动漫风格图片生成")
    public R<Img2ImgResponse> animeStyle(
            @ApiParam(value = "Img2Img请求参数", required = true)
            @Valid @RequestBody Img2ImgRequest request) {
        
        log.info("收到动漫风格转换请求: 提示词：{}，负面提示词：{}，图片的长度：{}", 
                request.getPrompt(), request.getNegativePrompt(), request.getInitImage().length());
        
        try {
            Img2ImgResponse response = img2ImgService.animeStyleTransform(request);
            return R.ok(response);
        } catch (Exception e) {
            log.error("动漫风格转换失败", e);
            return R.error("动漫风格转换失败: " + e.getMessage());
        }
    }

    /**
     * 执行吉卜力风格图片生成
     */
    @PostMapping("/ghibli")
    @ApiOperation("执行宫崎骏风格图片生成")
    public R<Img2ImgResponse> ghibliStyle(
            @ApiParam(value = "Img2Img请求参数", required = true)
            @Valid @RequestBody Img2ImgRequest request) {
        
        log.info("收到宫崎骏风格转换请求: 提示词：{}，负面提示词：{}，图片的长度：{}",
                request.getPrompt(), request.getNegativePrompt(), request.getInitImage().length());
        
        try {
            Img2ImgResponse response = img2ImgService.ghibliStyleTransform(request);
            return R.ok(response);
        } catch (Exception e) {
            log.error("宫崎骏风格转换失败", e);
            return R.error("宫崎骏风格转换失败: " + e.getMessage());
        }
    }
    
    /**
     * 检查Flask API服务健康状态
     */
    @GetMapping("/health")
    @ApiOperation("检查Flask API服务健康状态")
    public R<Map<String, Object>> checkHealth() {
        try {
            Map<String, Object> healthInfo = img2ImgService.checkServiceHealth();
            return R.ok(healthInfo);
        } catch (Exception e) {
            log.error("健康检查失败", e);
            return R.error("健康检查失败: " + e.getMessage());
        }
    }

    /**
     * 根据任务ID查询生成记录
     */
    @GetMapping("/task/{taskId}")
    @ApiOperation("根据任务ID查询生成记录")
    public R<Img2Img> getByTaskId(
            @ApiParam(value = "任务ID", required = true)
            @PathVariable String taskId) {
        
        try {
            Img2Img record = img2ImgService.getByTaskId(taskId);
            if (record != null) {
                return R.ok(record);
            } else {
                return R.error("未找到任务ID为 " + taskId + " 的记录");
            }
        } catch (Exception e) {
            log.error("查询任务记录失败", e);
            return R.error("查询任务记录失败: " + e.getMessage());
        }
    }
    
    /**
     * 分页查询生成记录
     */
    @GetMapping("/page")
    @ApiOperation("分页查询生成记录")
    public R<PageDTO<Img2Img>> getPage(
            @ApiParam(value = "页码", required = true, example = "1")
            @RequestParam(defaultValue = "1") int page,
            @ApiParam(value = "每页大小", required = true, example = "10")
            @RequestParam(defaultValue = "10") int size) {
        
        try {
            PageDTO<Img2Img> records = img2ImgService.getPage(page, size);
            return R.ok(records);
        } catch (Exception e) {
            log.error("分页查询失败", e);
            return R.error("分页查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取统计信息
     */
    @GetMapping("/stats")
    @ApiOperation("获取统计信息")
    public R<Map<String, Object>> getStats() {
        try {
            Map<String, Object> stats = img2ImgService.getStats();
            return R.ok(stats);
        } catch (Exception e) {
            log.error("获取统计信息失败", e);
            return R.error("获取统计信息失败: " + e.getMessage());
        }
    }
} 